var breadCrumb = new BreadCrumb();
var ctrlMenu = null;
var groupCard = null;

var parentId = getUrlParam('parentId');
var loadData = null;
var delData = null;
var createUrl = null;
var saveUrl = null;
var loadPage = null;

function  init() {
  loadPage = new LoadPage();
  loadPage.show($('#content'));
  var contentHeight = $(window).height()-$('#bar').css('height').replace('px','')-4;
  $('#content').css('height',contentHeight+'px');
  var data = [
    {
      'action':'rootAction(\'0\');',
      'title':' '
    }
  ];
  breadCrumb.setData(data);
  breadCrumb.setContainer($('#floderRoot'));
  breadCrumb.create();
}

function rootAction(id)
{
  closeCtrl();
  loadData(id);
}

function openCtrl() {
  var menuList = [
    {
      'action': 'upload();',
      'title': '上传文件'
    },
    {
      'action': 'createGroup();',
      'title': '创建组'
    }
  ];
  ctrlMenu = createBoxCardMenuDown('ctrlMenu', menuList, $('#ctrlButton'), 100);
}

function upload()
{
  closeCtrl();
  var style = {
    'width':'80%',
    'height':'80%',
    'top':'10px',
    'left':'10px',
    'position':'absolute'
  };
  groupCard = new BoxCard();
  groupCard.setId('fileInfo');
  groupCard.setSrc('//pages/hadeslist/file.html?createUrl='+createUrl+'&saveUrl='+saveUrl+'&parentId='+parentId+'&cancelback=removeGroup&okback=reload');
  groupCard.setStyle(style);
  var topBody = getMainBody(window);
  groupCard.setParentElement(topBody);
  groupCard.create();
}

function createGroup()
{
  closeCtrl();
  var style = {
    'width':'400',
    'height':'200',
    'top':'10px',
    'left':'10px',
    'position':'absolute'
  };
  groupCard = new BoxCard();
  groupCard.setId('groupInfo');
  groupCard.setSrc('//pages/hadeslist/group.html?createUrl='+createUrl+'&saveUrl='+saveUrl+'&parentId='+parentId+'&cancelback=removeGroup&okback=reload');
  groupCard.setStyle(style);
  var topBody = getMainBody(window);
  groupCard.setParentElement(topBody);
  groupCard.create();
}

function reload()
{
  loadData(parentId);
  removeGroup();
}

function removeGroup()
{
  if(groupCard!=null){
    groupCard.remove();
    groupCard = null;
  }
}

function openChildCtrl(fileType) {
  var menuList = [
    {
      'action': 'openChild(\''+fileType.id+'\',\''+fileType.name+'\');',
      'title': '打开'
    },
    {
      'action': 'edit(\''+hateoasGetUrl(fileType.links,'self')+'\');',
      'title': '重命名'
    },
    {
      'action': 'del(\''+hateoasGetUrl(fileType.links,'delete')+'\');',
      'title': '删除'
    }
  ];
  ctrlMenu = createBoxCardMenuDown('ctrlMenu', menuList, $('#folder_'+fileType.id), 100);
}

function openChild(id,name)
{
  var data = {
    'action':'rootAction(\''+id+'\');',
    'title':name
  };
  closeCtrl();
  loadData(id);
  breadCrumb.add(data);
}

function edit(getUrl)
{
  closeCtrl();
  var style = {
    'width':'400',
    'height':'200',
    'top':'10px',
    'left':'10px',
    'position':'absolute'
  };
  groupCard = new BoxCard();
  groupCard.setId('groupInfo');
  groupCard.setSrc('//pages/hadeslist/group.html?createUrl='+createUrl+'&saveUrl='+saveUrl+'&getUrl='+getUrl+'&parentId='+parentId+'&cancelback=removeGroup&okback=reload');
  groupCard.setStyle(style);
  var topBody = getMainBody(window);
  groupCard.setParentElement(topBody);
  groupCard.create();
}

function del(url)
{
  closeCtrl();
  delData(url);
}

function doCtrl()
{
  if(ctrlMenu == null)
    openCtrl();
  else{
    closeCtrl();
  }
}

function closeCtrl() {
  if(ctrlMenu != null) {
    ctrlMenu.remove();
    ctrlMenu = null;
  }
}

function goBack()
{
  breadCrumb.goBack();
}

var app = angular.module('cancer', []);
app.controller('siteCtrl', function($scope, $http) {
  $scope.loadData = function(id) {
    $http({
      method: 'POST',
      url: 'http://192.168.1.14/hades/group/' + id
    }).then(function successCallback(response) {
      $scope.group = response.data;
      createUrl = hateoasGetUrl($scope.group.links,'create');
      saveUrl = hateoasGetUrl($scope.group.links,'save');
      $scope.loadComposites(hateoasGetUrl($scope.group.links,'findComposites'));
      parentId = id;
    }, function errorCallback(response) {
      // 请求失败执行代码
    });
  }
  $scope.loadComposites = function(findurl) {
    $http({
      method: 'POST',
      url: findurl
    }).then(function successCallback(response) {
      if(loadPage != null)
        loadPage.hide();
      $scope.fileTypes = response.data;
    }, function errorCallback(response) {
      // 请求失败执行代码
    });
  }

  $scope.ngDoCtrl = function(fileType){
    if(ctrlMenu == null)
      openChildCtrl(fileType);
    else{
      closeCtrl();
    }
  }

  $scope.delData = function(delurl) {
    $http({
      method: 'POST',
      url: delurl
    }).then(function successCallback(response) {
      $scope.loadData(parentId);
    }, function errorCallback(response) {
      // 请求失败执行代码
    });
  }

  loadData = $scope.loadData;
  delData = $scope.delData;
  $scope.loadData(parentId);
});
